package com.my.blog.website.controller.user;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.my.blog.website.constant.RedisConst;
import com.my.blog.website.constant.WebConst;
import com.my.blog.website.controller.BaseController;
import com.my.blog.website.dto.MetaDto;
import com.my.blog.website.modal.Vo.*;
import com.my.blog.website.service.*;
import com.my.blog.website.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 首页
 * Created by Administrator on 2019/12/8 008.
 */
@Controller
public class IndexController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private IContentService contentService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IMetaService metaService;

    @Autowired
    private ISiteService siteService;


    /**
     * 首页
     *
     * @return
     */
    @GetMapping(value = {"/", "index"})
    public String index(HttpServletRequest request, @RequestParam(value = "limit", defaultValue = "8") int limit) {
        return this.index(request, 1, limit);
    }

    /**
     * 首页分页
     *
     * @param request request
     * @param p       第几页
     * @param limit   每页大小
     * @return 主页
     */
    @GetMapping(value = "page/{p}")
    public String index(HttpServletRequest request, @PathVariable int p, @RequestParam(value = "limit", defaultValue = "8") int limit) {
        p = p < 0 || p > WebConst.MAX_PAGE ? 1 : p;

        PageInfo<ContentVo> articles = contentService.getContents(p, limit);
        request.setAttribute("recommendBlogs", siteService.recentContents(6));

        List<MetaDto> types = metaService.listTypeTop("category", 6);
        request.setAttribute("types", types);

        String json = stringRedisTemplate.opsForValue().get(RedisConst.REDIS_TAGS);

        //在这做redis插入json,明天做
        if (json == null || json.length() == 0) {
            List<MetaVo> tags = metaService.getMetas("tag");
            json = JSON.toJSONString(tags);
            log.info("查询数据库");
            stringRedisTemplate.opsForValue().set(RedisConst.REDIS_TAGS, json);
        }
        request.setAttribute("tags", json);


        request.setAttribute("articles", articles);
        if (p > 1) {
            this.title(request, "第" + p + "页");
        }
        return this.render("index");
    }

    /**
     * footer
     */

    @GetMapping("/footer/newblog")
    public String newblogs(Model model) {
        model.addAttribute("newblogs", siteService.recentContents(3));
        return "_fragments :: newblogList";
    }


    /**
     * 注销
     *
     * @param session
     * @param response
     */
    @RequestMapping("logout")
    public void logout(HttpSession session, HttpServletResponse response) {
        TaleUtils.logout(session, response);
    }


    /**
     * 设置cookie
     *
     * @param name
     * @param value
     * @param maxAge
     * @param response
     */
    private void cookie(String name, String value, int maxAge, HttpServletResponse response) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAge);
        cookie.setSecure(false);
        response.addCookie(cookie);
    }

}
